package com.bw.zg2.view.fragment

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.fragment.app.Fragment
import com.bumptech.glide.Glide
import com.bw.zg2.R
import com.bw.zg2.utils.DbUtils
import com.bw.zg2.utils.GlideUtil
import com.bw.zg2.utils.SpUtil
import com.bw.zg2.view.activity.LoginActivity
import com.bw.zg2.view.activity.TestActivity

/**
 * create an instance of this fragment.
 */
class MineFragment : Fragment() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        val v = inflater.inflate(R.layout.fragment_mine, container, false)
        v.findViewById<TextView>(R.id.tv_logout).setOnClickListener {
            SpUtil.clearData()
            startActivity(Intent(activity, LoginActivity::class.java))
            activity?.finish()
        }

        v.findViewById<TextView>(R.id.tv_test).setOnClickListener {
            startActivity(Intent(activity, TestActivity::class.java))
        }

        //从数据库获取loginBean
        val loginBean = DbUtils.shopDB.getLoginBeanDao().getLogin()
        val tvName = v.findViewById<TextView>(R.id.tv_username)
        val tvSex = v.findViewById<TextView>(R.id.tv_sex)
        val tvMoney = v.findViewById<TextView>(R.id.tv_money)
        val ivUser = v.findViewById<ImageView>(R.id.iv_user)


//        tvName.setText( SpUtil.getString("name"))
        tvName.setText("姓名：${loginBean.username}")
        tvSex.setText("性别：男")
        tvMoney.text ="账户余额：${loginBean.money} 元"

        GlideUtil.loadCircleImg(requireActivity(), loginBean.icon, ivUser)

        return v
    }
}